<template>
	<view class="group_search">
		<view class="group_search_top_box">
			<view class="group_search_box">
				<view class="group_search_input_box">
					<uni-icons type="search" color="#999999" size="24"></uni-icons>
					<input class="group_search_input flex-1" type="text" confirm-type="search" v-model="params.content"
						placeholder="输入搜索关键词" @confirm="searchClub" />
					<uni-icons v-if="params.content" type="clear" color="#999999" size="18" style="padding: 20rpx;"
						@click="clearContent"></uni-icons>
				</view>
				<view class="group_search_btn" @click="searchClub">搜索</view>
			</view>
		</view>
		<view class="search_logs_box" v-if="showLogs">
			<view class="search_logs_top_cell" v-if="logtotal>0">
				<view class="f-12 flex-1 c-999999">最近搜索</view>
				<view class="f-12 c-999999" @click="clearSearchLogs">清空</view>
			</view>

			<view class="search_logs_cell" v-for="(item,index) in loglist" :key="item.id"
				@click="bindLogs(item.content)">
				<view class="f-13">{{item.content}}</view>
			</view>
			<view class="search_logs_cell" v-if="showMoreText" style="justify-content: center;" @click="showMoreLog">
				<view class="f-12 text-c c-999999">查看更多</view>
				<uni-icons type="down" color="#999999" size="14"></uni-icons>
			</view>
			<view class="search_logs_cell" v-if="logtotal==0" style="justify-content: center;">
				<view class="f-12 text-c c-999999">暂无搜索记录</view>
			</view>
		</view>

		<view class="group_cell_box" v-else>
			<view class="group_cell" v-for="(item,index) in list" :key="index" @click="goinfo(item.club_id)">
				<image class="group_cell_img" :src="picUrl+item.pic" mode="aspectFill"></image>
				<view class="group_mask_box" v-if="item.pic"></view>
				<view class="group_content_box">
					<view class="group_carmodel">{{item.car_model?item.car_model:'SUBARU'}}</view>
					<view class="group_name">{{item.title}}</view>
				</view>
				<view class="join_is_my_box"
					:style="item.is_my==1?'background: linear-gradient(to right, #00A0DC, #0E60E7);':'background-color: rgba(0, 0, 0, 0.75);'">
					<view class="display_flex">
						<view class="display_flex flex-1">
							<view class="display_flex">
								<image class="group_mask_icon"
									:src="item.is_my==1?onlinePic+'group_user_icon_w.png':onlinePic+'group_user_icon.png'">
								</image>
								<view class="ml-5 c-white f-10">{{item.join_count}} 成员</view>
							</view>
							<view class="display_flex ml-15">
								<image class="group_mask_icon"
									:src="item.is_my==1?onlinePic+'group_active_icon_w.png':onlinePic+'group_active_icon.png'">
								</image>
								<view class="ml-5 c-white f-10">{{item.huodong_count}} 场活动</view>
							</view>
						</view>
						<view class="display_flex f-10 c-white">
							<view v-if="item.is_my==1">已加入</view>
							<view v-if="item.is_my==1" class="ml-15 mr-15">|</view>
							<view>查看详情</view>
						</view>
					</view>
				</view>
				<view class="trapezium_img_boxd" v-if="item.city_name">
					<view class="trapezium_img_box">
						<image class="trapezium_img" :src="picUrl+item.city_icon"></image>
						<view class="trapezium_text">
							{{item.city_name.length>8?item.city_name.substring(0,8):item.city_name}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view v-if="!showLogs" class="page_bottom_loading" style="padding-bottom:50rpx;">{{loadingTitle}}</view>
	</view>
</template>

<script>
	import {
		getSearchLogs,
		getSearchClubList,
		clearSearchLogs,
	} from '@/api/motortime.js';
	let app = getApp()
	export default {
		data() {
			return {
				logparams: {
					page: 1,
				},
				logtotal: '',
				params: {
					content: '',
					page: 1,
					size: 10,
					city_id: ''
				},
				showLogs: true,
				showMoreText: false,
				loglist: [],
				list: [],
				loadingTitle: '',
				picUrl: app.globalData.picUrl,
				onlinePic: app.globalData.onlinePic,
			};
		},
		onShow() {
			if (uni.getStorageSync('onload')) {
				uni.showLoading({})
				this.params.page = 1
				this.list = []
				this.loglist = []
				uni.removeStorage({
					key: 'onload'
				})
			}
		},
		onLoad() {
			if (uni.getStorageSync('access_token')) {
				this.params.page = 1
				this.list = []
				this.mygrouplist = []
				this.city_id = uni.getStorageSync('clubconfig') ? uni.getStorageSync('clubconfig').city_id : ''
				this.params.city_id = uni.getStorageSync('clubconfig') ? uni.getStorageSync('clubconfig').city_id : ''
				this.getSearchLogs()
			} else {
				uni.navigateTo({
					url: '/pages/login/login'
				})
				this.loadingTitle = "- 暂无更多 -"
			}
			var params = {
				event_code: '车友会搜索',
				path: 'pages/group/groupsearch',
				event_id: '',
				title: '',
				source_page: '',
				value: '',
			}
			app.BurialPoint(params)
		},
		methods: {
			getSearchLogs() {
				getSearchLogs(this.logparams).then(res => {
					if (res.state == 1) {
						this.loglist = [...this.loglist, ...res.data.list.data]
						this.logtotal = res.data.list.total
						if (res.data.list.last_page == res.data.list.current_page) {
							this.showMoreText = false
						} else {
							this.showMoreText = true
						}
					} else {
						uni.showToast({
							title: res.error[0],
							mask: true,
							icon: 'none',
							duration: 1500
						})
					}
				})
			},
			bindLogs(content) {
				this.params.content = content
				this.params.page = 1
				this.list = []
				this.getSearchClubList()
				var params = {
					event_code: '车友会搜索',
					path: 'pages/group/groupsearch',
					event_id: '',
					title: 'content',
					source_page: '点击历史搜索',
					value: '',
				}
				app.BurialPoint(params)
			},
			clearContent() {
				this.params.content = ''
				this.params.page = 1
				this.showLogs = true
				this.list = []
				this.loglist = []
				this.logparams.page = 1
				this.getSearchLogs()
			},
			showMoreLog() {
				this.logparams.page = 2
				this.getSearchLogs()
			},
			searchClub() {
				if (this.params.content) {
					uni.showLoading({})
					this.params.page = 1
					this.list = []
					this.getSearchClubList()
					var params = {
						event_code: '车友会搜索',
						path: 'pages/group/groupsearch',
						event_id: '',
						title: '',
						source_page: '点击搜索',
						value: '',
					}
					app.BurialPoint(params)
					this.loglist = []
					this.getSearchLogs()
				} else {
					this.showLogs = true
					this.list = []
				}

			},
			getSearchClubList() {
				getSearchClubList(this.params).then(res => {
					if (res.state == 1) {
						this.showLogs = false
						this.list = [...this.list, ...res.data.data]
						this.current_page = res.data.current_page
						this.last_page = res.data.last_page
						if (this.list.length == 0) {
							this.loadingTitle = "- 暂无更多 -"
						}
						if (res.data.total < 10) {
							this.loadingTitle = "- 暂无更多 -"
						}
						uni.hideLoading()
					} else {
						this.loadingTitle = "- 暂无更多 -"
						uni.showToast({
							title: res.error[0],
							mask: true,
							icon: 'none',
							duration: 1500
						})
					}
				})
			},
			clearSearchLogs() {

				clearSearchLogs({}).then(res => {
					if (res.state == 1) {
						this.logparams.page = 1
						this.loglist =[]
						this.getSearchLogs()
					} else {
						uni.showToast({
							title: res.error[0],
							mask: true,
							icon: 'none',
							duration: 1500
						})
					}
				})
			},
			goinfo(club_id) {
				var params = {
					event_code: '车友会',
					path: 'pages/group/group',
					event_id: club_id,
					title: '',
					source_page: '点击车友会',
					value: '',
				}
				app.BurialPoint(params)
				uni.navigateTo({
					url: '/pages/group/groupinfo?club_id=' + club_id
				})

			}
		},
		onPullDownRefresh() {
			if(this.params.content){
				this.params.page = 1
				this.list = []
				this.getSearchClubList()
			}
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.current_page < this.last_page) {
				this.showLoading = true
				this.params.page = this.params.page + 1
				this.getOtherCityClubList()
			} else {
				this.showLoading = false
				if (this.list.length > 0) {
					this.loadingTitle = "- 已经到底了 -"
				} else {
					this.loadingTitle = "- 暂无更多 -"
				}

			}
		},
		onShareAppMessage(res) {
			app.getShareTask()
			return {
				title: '斯巴鲁官方车友会',
				imageUrl: this.list.length > 0 ? this.picUrl + this.list[0].pic : ''
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f7f7f7;
	}

	.group_search {
		.group_search_top_box {
			position: sticky;
			top: 0;
			left: 0;
			right: 0;
			z-index: 10;
			padding: 30rpx;
			background-color: #f7f7f7;

			.group_search_box {
				height: 90rpx;
				display: flex;
				align-items: center;

				.group_search_input_box {
					flex: 1;
					display: flex;
					align-items: center;
					padding: 0 0 0 20rpx;
					background-color: #ffffff;
					border: 1rpx solid #DCDCDC;
					border-top-left-radius: 8rpx;
					border-bottom-left-radius: 8rpx;

					.group_search_input {
						height: 90rpx;
						font-size: 32rpx;
						margin-left: 20rpx;

					}
				}

				.group_search_btn {
					width: 135rpx;
					height: 92rpx;
					font-size: 32rpx;
					color: #ffffff;
					display: flex;
					align-items: center;
					justify-content: center;
					background-color: #00A0DC;
					border-top-right-radius: 8rpx;
					border-bottom-right-radius: 8rpx;
				}
			}
		}

		.search_logs_box {
			padding: 0 30rpx;

			.search_logs_top_cell {
				display: flex;
				align-items: center;
				padding: 20rpx;
				border-bottom: 1rpx solid #DCDCDC;
			}

			.search_logs_cell {
				display: flex;
				align-items: center;
				padding: 30rpx 20rpx;
				border-bottom: 1rpx solid #DCDCDC;
			}

			.search_logs_cell:last-child {
				border-bottom: none;
			}
		}

		.group_tabs_box {
			padding: 30rpx;
			position: sticky;
			top: 0;
			z-index: 5;
			background-color: #f7f7f7;

			.join_group_img {
				width: 100%;
				height: 110rpx;
				vertical-align: middle;
			}
		}

		.group_cell_box {
			padding: 10rpx 30rpx;

			.group_cell {
				width: 100%;
				position: relative;
				margin-bottom: 30rpx;

				.join_is_my_box {
					position: absolute;
					left: 0;
					bottom: 0;
					right: 0;
					padding: 18rpx 50rpx;
					z-index: 2;
					border-bottom-right-radius: 8rpx;
					border-bottom-left-radius: 8rpx;

					.group_mask_icon {
						width: 25rpx;
						height: 25rpx;
					}

					.group_check {
						width: 114rpx;
						height: 36rpx;
					}
				}

				.group_cell_img {
					width: 100%;
					height: 340rpx;
					vertical-align: middle;
					border-radius: 8rpx;
				}

				.group_mask_box {
					position: absolute;
					top: 0;
					right: 50%;
					bottom: 0;
					left: 0;
					z-index: 1;
					background: linear-gradient(to right, rgba(0, 0, 0, 0.85), rgba(0, 0, 0, 0));
					border-top-left-radius: 8rpx;
					border-bottom-left-radius: 8rpx;
				}

				.group_content_box {
					position: absolute;
					top: 0;
					right: 0;
					bottom: 0;
					left: 0;
					z-index: 2;
					padding: 40rpx 50rpx;

					.group_carmodel {
						color: #ffffff;
						font-size: 26rpx;
						letter-spacing: 1rpx;
					}

					.group_carmodel_bottom {
						width: 20rpx;
						height: 4rpx;
						margin-top: 10rpx;
						background-color: #00A0DC;
					}

					.group_name {
						color: #ffffff;
						font-size: 32rpx;
						font-weight: bold;
						margin-top: 5rpx;
						letter-spacing: 1rpx;
					}

					.group_mask_icon {
						width: 25rpx;
						height: 25rpx;
					}

					.group_check {
						width: 114rpx;
						height: 36rpx;
					}
				}
			}
		}


	}
</style>